package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author WFH
 * @since 2020-10-09
 */
@Api(description="课程基本信息管理")
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin//解决跨域
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //课程列表 基本实现
    //TODO  完善条件查询带分页
    @ApiOperation(value = "分页查询课程的方法")
    @PostMapping("/pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable long current,@PathVariable long limit,
                        @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> pageTeacher = new Page<>(current,limit);

        //调用方法实现分页
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        //对前端传来的条件进行判断,column是数据库字段名，而不是实体属性名
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //条件进行判断，如果符合则拼接
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status) ) {
            wrapper.eq("status", status);
        }
        //根据时间降序排序
        wrapper.orderByDesc("gmt_create");

        //调用方法的时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        //其实返回值对象的page里面也有分页所有数据，个人比较喜欢用page
        IPage<EduCourse> page = courseService.page(pageTeacher, wrapper);
        long total = page.getTotal();//总记录数
        List<EduCourse> records = page.getRecords();//数据list集合
        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);//list
        return R.ok().data(map);
    }

    //添加课程基本信息的方法
    @ApiOperation(value = "添加课程基本信息方法")
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后课程id，为了后面添加大纲使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @ApiOperation(value = "根据课程id查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @ApiOperation(value = "根据课程ID进行最终确认")
    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    //修改课程状态，课程最终发布
    @ApiOperation(value = "修改课程状态，课程最终发布")
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");//设置课程发布状态(Normal是发布状态 Draft是未发布状态)
        courseService.updateById(eduCourse);
        return R.ok();
    }

    //删除课程
    @ApiOperation(value = "删除课程")
    @DeleteMapping("/{courseId}")
    public R deleteCourse(@PathVariable String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }

}

